package com.example.academic_affairs_system.controller;

import com.example.academic_affairs_system.domian.dto.LoginDTO;
import com.example.academic_affairs_system.domian.pojo.Admin;
import com.example.academic_affairs_system.service.AdminService;
import com.example.academic_affairs_system.util.model.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/admin")
@Api(tags = "管理员接口")
public class AdminController {

    @Resource
    private AdminService adminService;

    @PostMapping("/login")
    @ApiOperation(value = "管理员登录")
    public R login(@Validated LoginDTO loginDTO, HttpSession session) {
        System.out.println(loginDTO);
        Admin admin = adminService.login(loginDTO.getName(), loginDTO.getPassword());

        if (admin != null) {
            session.setAttribute("adminName", admin.getAdminName());
            return R.ok(admin);
        }else {
            return R.error("用户名或密码错误");
        }

    }

    @PostMapping("/out")
    @ApiOperation(value = "管理员登出")
    public R out(HttpSession session) {
        session.removeAttribute("adminName");
        return R.ok("登出成功");
    }

}
